package com.vlong.town.controller.admin.honour;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.honour.HonourInfo;
import com.vlong.town.service.honour.HonourInfoService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

/**
 * 荣誉成就信息Controller
 * @author Assassin
 */
@Controller
@RequestMapping("/honour")
public class HonourInfoController extends BaseController {

    // Fields

    @Autowired
    private HonourInfoService honourInfoService;// 荣誉成就信息业务实现类

    /**
     * 进入荣誉成就信息列表页面
     * @return honour/list_honour
     */
    @RequestMapping("/list")
    public String gotoHonourInfoPage(
            @RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="title") String title,
            @RequestParam(required=false, value="content") String content,
            @RequestParam(required=false, value="desc") String desc) {
        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<HonourInfo> honourInfoList = this.honourInfoService.queryList(title, content, desc);
        PageInfo<HonourInfo> pageInfo = new PageInfo<HonourInfo>(honourInfoList);
        PageTag<HonourInfo> pt = new PageTag<HonourInfo>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("title", title);
        request.setAttribute("content", content);
        request.setAttribute("desc", desc);
        for(HonourInfo honourInfo : honourInfoList) {
            honourInfo.setContent(DataUtil.delHTMLTag(honourInfo.getContent(), 0));
        }
        request.setAttribute("honourInfoList", honourInfoList);
        return "admin/honour/list_honour";
    }

    /**
     * 进入荣誉成就信息添加页面
     * @return honour/add_honour
     */
    @GetMapping("/addHonour")
    public String gotoHonourInofAddPage() {
        return "admin/honour/add_honour";
    }

    /**
     * 进入荣誉成就信息修改页面
     * @return honour/edit_honour
     */
    @GetMapping("/editHonour/{honourId}")
    public String gotoHonourInofEditPage(@PathVariable(required=true, value="honourId") Long honourId) {
        HonourInfo honourInfo = this.honourInfoService.queryById(honourId);
        request.setAttribute("honourInfo", honourInfo);
        request.setAttribute("thumbnail",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + honourInfo.getThumbnail());
        return "admin/honour/edit_honour";
    }

    /**
     * 添加荣誉成就信息
     * @param title 荣誉成就标题
     * @param content 荣誉成就内容
     * @param desc 荣誉成就简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeAddHonour")
    public DataMessage addHonour(
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("荣誉成就标题不合法");
            return dm;
        }
        this.honourInfoService.add(1L, title, content, desc, picture);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("荣誉成就添加成功");
        return dm;
    }

    /**
     * 修改荣誉成就信息
     * @param title 荣誉成就标题
     * @param content 荣誉成就内容
     * @param desc 荣誉成就简介
     * @param picture 缩略图
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeEditHonour")
    public DataMessage editHonour(
            @RequestParam(name="honourId", required=true) Long honourId,
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("荣誉成就标题不合法");
            return dm;
        }
        this.honourInfoService.update(honourId, title, content, desc, picture);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("荣誉成就修改成功");
        return dm;
    }

    /**
     * 删除荣誉成就
     * @param honourId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/delete")
    public DataMessage deleteUser(@RequestParam(required=true, value="honourId") Long honourId) {
        DataMessage dm = new DataMessage();
        if(honourId != null && honourId > 0L) {
            this.honourInfoService.delete(honourId);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("荣誉成就删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

    /**
     * 批量删除荣誉成就
     * @param honourIds 主键值集合字符串
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/batchDelete")
    public DataMessage batchDeleteUser(@RequestParam(required=true, value="honourIds") String honourIds) {
        DataMessage dm = new DataMessage();
        if(this.validateStringProperty(honourIds, 1, 255)) {
            String[] honourIdArray = honourIds.split(",");
            for(String honourId : honourIdArray) {
                this.honourInfoService.delete(Long.parseLong(honourId));
            }
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("荣誉成就删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

}
